package com.zxl.live.tools.tools;

import android.app.Activity;
import android.os.Build;
import android.view.View;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 魅族SmartBar的工具类
 * @date  [2016年5月1日]
 */
public class SmartBarUtils {
	
	private static final boolean MEIZU_SMARTBAR;
	private static Field sHideNavigationField;
	private static Method sSetSystemUiVisibilityMethod;
	
	static {
		MEIZU_SMARTBAR = hasSmartBar();
		if (MEIZU_SMARTBAR) {
			try {
				sSetSystemUiVisibilityMethod = View.class.getMethod("setSystemUiVisibility",
						Integer.TYPE);
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
			try {
				sHideNavigationField = View.class.getField("SYSTEM_UI_FLAG_HIDE_NAVIGATION");
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
		}
	}

	public static boolean hasSmartBar() {
		try {
			// 新型号可用反射调用Build.hasSmartBar()
			Method method = Class.forName("android.os.Build").getMethod("hasSmartBar");
			return (Boolean) method.invoke(null);
		} catch (Exception ignored) {
		}

		// 反射不到Build.hasSmartBar()，则用Build.DEVICE判断
		return Build.DEVICE.equals("mx2") || Build.DEVICE.equals("mx3");
	}
	
	public static void hideSmartBar(Activity activity) {
		View decorView = activity.findViewById(android.R.id.content);
		hideSmartBar(decorView);
	}

	public static void hideSmartBar(View decorView) {
		if (!MEIZU_SMARTBAR || decorView == null) {
			return;
		}

		if (sSetSystemUiVisibilityMethod != null && sHideNavigationField != null) {
			try {
				Object[] args = new Object[1];
				try {
					args[0] = sHideNavigationField.get(null);
				} catch (Exception ignored) {
				}

				sSetSystemUiVisibilityMethod.invoke(decorView, args);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
